import "package:flutter/material.dart";
import '../style/color.dart';
import '../style/text.dart';
import '../style/decoration.dart';

const supportInputType = ['text', 'number', 'password'];
// 通用组合输入框
class CommonInput extends StatelessWidget {
  CommonInput(
      {@required this.type,
      this.onChanged,
      this.placeholder,
      @required this.title,
      this.needAutoFocus = false,
      this.onFocus})
      : assert(supportInputType.contains(type));

  final String type; // 输入框类型
  final String title; // 输入框标题
  final String placeholder; // 占位符文本
  final ValueChanged<String> onChanged; // 文本框内容变化时执行的回调
  final ValueChanged<bool> onFocus; // 文本内容获得焦点时执行的回调
  final bool needAutoFocus; // 是否需要自动聚焦

  // 构造标题私有方法
  Container _buildTitle() {
    return Container(
      padding: EdgeInsets.only(left: 15),
      alignment: Alignment.centerLeft,
      width: 100,
      child: Text(title, style: TextStyle(fontSize: 16)),
    );
  }

  // 构造表单域私有方法
  TextField _buildInput() {
    return TextField(
      key: ValueKey(title),
      onChanged: onChanged,
      obscureText: type == 'password', // 是否隐藏输入
      autofocus: needAutoFocus,
      cursorColor: mainThemeColor,
      keyboardType:
          type == 'number' ? TextInputType.number : TextInputType.text, // 键盘类型
      style: normalInputStyle,
      decoration: inputDecoration(placeholder),
    );
  }

  // 构造下划线私有方法
  Padding _buildDivider() {
    return Padding(
      padding: EdgeInsets.only(left: 15),
      child: Divider(
        height: 1,
        thickness: 0.5,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Row(
          children: <Widget>[
            _buildTitle(),
            Expanded(
                child: Focus(onFocusChange: onFocus, child: _buildInput())),
          ],
        ),
        _buildDivider()
      ],
    );
  }
}
